from celery import shared_task
from .app import app


def fake_compute(dataset):
    import copy
    import time
    time.sleep(30)
    return copy.deepcopy(dataset)


@shared_task(name='zmm.do_compute')
def on_task_compute(payload):
    try:
        # 通知计算任务已经开始
        app.send_task('zmm.do_start', [payload])

        # 执行计算
        # TODO：实现真正的计算逻辑，现在搞个假的
        result = fake_compute(payload['input_dataset'])

        # 通知计算任务已经结束
        payload['result_dataset'] = result
        app.send_task('zmm.do_finish', [payload])
    except Exception:
        # 通知计算任务已经出错
        app.send_task('zmm.do_fail', [payload])

